package com.xty.xtyle.frames;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.xty.xtyle.R;
import com.xty.xtyle.base.AbsFragment;
import com.xty.xtyle.base.FmPagerAdapter;
import com.xty.xtyle.base.TabFragment;
import com.xty.xtyle.db.ClothTypes;
import com.xty.xtyle.db.ClothTypesDao;
import com.xty.xtyle.db.cloth.Cloth;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * 衣柜页面
 */
public class ClothFragment extends AbsFragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private List<ClothTypes> types;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_cloth;
    }

    @Override
    public void initView() {

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
        init();
    }



    @Override
    public void freshData() {

    }



    /**
     * 查询衣服的类型，
     */
    private void init() {

        //数据库查找衣服类型
        ClothTypesDao dao = new ClothTypesDao();
        types = dao.findAll();

        //通过类型来创建tablayout, 具体的衣服列表通过tabFragment获取展示
        for(int i=0;i<types.size();i++){
            TabFragment tabFragment = new TabFragment();
            Bundle args = new Bundle();
            args.putString("type", types.get(i).getType());
            tabFragment.setArguments(args);
            fragments.add(tabFragment);
            tabLayout.addTab(tabLayout.newTab());
        }

        //将数据绑定到列表空间上
        tabLayout.setupWithViewPager(viewPager,false);
        pagerAdapter = new FmPagerAdapter(fragments, getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        for(int i=0;i<types.size();i++){
            tabLayout.getTabAt(i).setText(types.get(i).getType());
        }
    }
}
